#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mosquitto.h"

#define HOST "mq.tongxinmao.com"
#define PORT 18830
#define KEEP_ALIVE 60
#define TOPIC "test/topic"
#define QOS 1

int mqtt_pub_test(void)
{
    mosquitto_lib_init();
    struct mosquitto *mosq = mosquitto_new("pub_client", true, NULL);

    if (mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)) {
        fprintf(stderr, "无法连接到代理\n");
        return 1;
    }
    else{printf("mqtt client connect success!");}

    const char *message = "helloworld";
    //mosquitto_publish(mosq, NULL, TOPIC, strlen(message), message, QOS, false);
    while(1)
    {
        mosquitto_publish(mosq, NULL, TOPIC, strlen(message), message, QOS, false);
        usleep(1000*1000);
    }
    
    // while (fgets(message, sizeof(message), stdin)) {
    //     mosquitto_publish(mosq, NULL, TOPIC, strlen(message), message, QOS, false);
    // }

    mosquitto_disconnect(mosq);
    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();
    return 0;
}

